package tw.com.softleader.fire.setting.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import tw.com.softleader.fire.common.dao.GenericDao;
import tw.com.softleader.fire.setting.entity.InsuredItemEntity;

/**
 * 
 * @author Yuanchieh(Ebi)
 * 
 */

@Repository
public class InsuredItemDao extends GenericDao<InsuredItemEntity> {

	@Autowired
	private SessionFactory sessionFactory;

	private static final String FIND_BY_CODE = "SELECT * FROM SET_INSUREDITEM WHERE CODE like :code";

	public List<InsuredItemEntity> findByCode(String code) {
		Session session = sessionFactory.getCurrentSession();

		StringBuffer sb = new StringBuffer();
		
		if( code != null) {
			sb.append(code);
		}
		
		sb.append("%");

		Query query = session.createSQLQuery(FIND_BY_CODE).addEntity(
				InsuredItemEntity.class);

		List<InsuredItemEntity> models = query.setString("code",
				sb.toString()).list();

		return models;
	}
}
